// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   check_ok.java

package manufacture.design_procedure;

import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataTag;

public class check_ok extends HttpServlet
{

	public check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 design_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 design_db1 = new nseer_db_backup1(dbApplication);
		counter count = new counter(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		ValidataTag vt = new ValidataTag();
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")) && design_db.conn((String)dbSession.getAttribute("unit_db_name")) && design_db1.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String config_id = request.getParameter("config_id");
				String procedure_amount = request.getParameter("procedure_amount");
				int num = Integer.parseInt(procedure_amount);
				String design_ID = request.getParameter("design_ID");
				String product_ID = request.getParameter("product_ID");
				String product_name = request.getParameter("product_name");
				String designer = request.getParameter("designer");
				String bodyc = new String(request.getParameter("procedure_describea").getBytes("UTF-8"), "UTF-8");
				String procedure_describea = exchange.toHtml(bodyc);
				String check_time = request.getParameter("check_time");
				String checker = request.getParameter("checker");
				String checker_ID = request.getParameter("checker_ID");
				int n = 0;
				for (int i = 1; i < num; i++)
				{
					String tem_labour_hour_amount = (new StringBuilder("labour_hour_amount")).append(i).toString();
					String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
					String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
					String labour_hour_amount = request.getParameter(tem_labour_hour_amount);
					String amount_unit = request.getParameter(tem_amount_unit);
					String cost_price2 = request.getParameter(tem_cost_price);
					if (labour_hour_amount.equals(""))
						labour_hour_amount = "0";
					if (cost_price2.equals(""))
						cost_price2 = "0";
					StringTokenizer tokenTO2 = new StringTokenizer(cost_price2, ",");
					String cost_price;
					String cost_price1;
					for (cost_price = ""; tokenTO2.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
						cost_price1 = tokenTO2.nextToken();

					if (!validata.validata(labour_hour_amount) || !validata.validata(cost_price))
						n++;
					if (amount_unit.indexOf("'") != -1 || amount_unit.indexOf("\"") != -1)
						n++;
				}

				String sql6 = (new StringBuilder("select id from manufacture_workflow where object_ID='")).append(design_ID).append("' and type_id='01' and ((check_tag='0' and config_id<'").append(config_id).append("') or (check_tag='1' and config_id='").append(config_id).append("'))").toString();
				ResultSet rs6 = manufacture_db.executeQuery(sql6);
				if (!rs6.next())
				{
					if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "manufacture_design_procedure", "design_ID", design_ID, "check_tag").equals("0"))
					{
						if (n == 0)
						{
							try
							{
								String sql = (new StringBuilder("update manufacture_design_procedure set product_ID='")).append(product_ID).append("',product_name='").append(product_name).append("',check_time='").append(check_time).append("',checker='").append(checker).append("',designer='").append(designer).append("',procedure_describe='").append(procedure_describea).append("',change_tag='0',design_module_tag='0' where design_ID='").append(design_ID).append("'").toString();
								manufacture_db.executeUpdate(sql);
								double cost_price_sum = 0.0D;
								for (int i = 1; i <= num; i++)
								{
									String tem_procedure_name = (new StringBuilder("procedure_name")).append(i).toString();
									String tem_procedure_ID = (new StringBuilder("procedure_ID")).append(i).toString();
									String tem_procedure_describe = (new StringBuilder("procedure_describe")).append(i).toString();
									String tem_labour_hour_amount = (new StringBuilder("labour_hour_amount")).append(i).toString();
									String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
									String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
									String procedure_name = request.getParameter(tem_procedure_name);
									String procedure_ID = request.getParameter(tem_procedure_ID);
									String procedure_describe = request.getParameter(tem_procedure_describe);
									String labour_hour_amount = request.getParameter(tem_labour_hour_amount);
									String amount_unit = request.getParameter(tem_amount_unit);
									String cost_price2 = request.getParameter(tem_cost_price);
									if (labour_hour_amount.equals(""))
										labour_hour_amount = "0";
									if (cost_price2.equals(""))
										cost_price2 = "0";
									StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
									String cost_price;
									String cost_price1;
									for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
										cost_price1 = tokenTO3.nextToken();

									double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(labour_hour_amount);
									cost_price_sum += subtotal;
									String sql1 = (new StringBuilder("update manufacture_design_procedure_details set procedure_ID='")).append(procedure_ID).append("',procedure_name='").append(procedure_name).append("',procedure_describe='").append(procedure_describe).append("',labour_hour_amount='").append(labour_hour_amount).append("',cost_price='").append(cost_price).append("',amount_unit='").append(amount_unit).append("',subtotal='").append(subtotal).append("' where design_ID='").append(design_ID).append("' and details_number='").append(i).append("'").toString();
									manufacture_db.executeUpdate(sql1);
								}

								String sql2 = (new StringBuilder("update manufacture_workflow set checker='")).append(checker).append("',checker_ID='").append(checker_ID).append("',check_time='").append(check_time).append("',check_tag='1' where object_ID='").append(design_ID).append("' and config_id='").append(config_id).append("'").toString();
								manufacture_db.executeUpdate(sql2);
								sql2 = (new StringBuilder("select id from manufacture_workflow where object_ID='")).append(design_ID).append("' and check_tag='0'").toString();
								ResultSet rset = manufacture_db.executeQuery(sql2);
								if (!rset.next())
								{
									String sql3 = (new StringBuilder("update manufacture_design_procedure_details set design_module_tag='0' where design_ID='")).append(design_ID).append("'").toString();
									manufacture_db.executeUpdate(sql3);
									String sql4 = (new StringBuilder("delete from manufacture_design_procedure_module_details where design_ID='")).append(design_ID).append("'").toString();
									manufacture_db.executeUpdate(sql4);
									String module_design_ID = "";
									String sql5 = (new StringBuilder("select * from design_module where product_ID='")).append(product_ID).append("' and check_tag='1' and excel_tag='2'").toString();
									ResultSet rs5 = design_db.executeQuery(sql5);
									if (rs5.next())
										module_design_ID = rs5.getString("design_ID");
									String sql8 = (new StringBuilder("select * from design_module_details where design_ID='")).append(module_design_ID).append("'").toString();
									String sql7;
									for (ResultSet rs8 = design_db1.executeQuery(sql6); rs8.next(); design_db.executeUpdate(sql7))
										sql7 = (new StringBuilder("update design_module_details set design_balance_amount='")).append(rs6.getString("amount")).append("' where id='").append(rs6.getString("id")).append("'").toString();

									sql2 = (new StringBuilder("update manufacture_design_procedure set cost_price_sum='")).append(cost_price_sum).append("',check_tag='1' where design_ID='").append(design_ID).append("'").toString();
									manufacture_db.executeUpdate(sql2);
								} else
								{
									sql2 = (new StringBuilder("update manufacture_design_procedure set cost_price_sum='")).append(cost_price_sum).append("' where design_ID='").append(design_ID).append("'").toString();
									manufacture_db.executeUpdate(sql2);
								}
							}
							catch (Exception ex)
							{
								ex.printStackTrace();
							}
							response.sendRedirect("manufacture/design_procedure/check_ok.jsp?finished_tag=0");
						} else
						{
							response.sendRedirect((new StringBuilder("manufacture/design_procedure/check_ok_a.jsp?design_ID=")).append(design_ID).toString());
						}
					} else
					{
						response.sendRedirect("manufacture/design_procedure/check_ok.jsp?finished_tag=1");
					}
				} else
				{
					response.sendRedirect("manufacture/design_procedure/check_ok.jsp?finished_tag=2");
				}
				design_db.commit();
				manufacture_db.commit();
				design_db1.commit();
				design_db.close();
				design_db1.close();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
